﻿using System.Linq;

namespace OpenWaves.ActivityFeed.Web
{
    public class PersonalActivityFeedBuilder : ActivityFeedBuilder
    {
        private readonly string _identityName;

        public PersonalActivityFeedBuilder(string identityName, IActivityFeedService activityFeedService, IUpdateDtoBuilder updateDtoBuilder)
            : base(activityFeedService, updateDtoBuilder)
        {
            this._identityName = identityName;
        }

        protected override IQueryable<Update> FilterUpdates(IQueryable<Update> updates)
        {
            var subscriber = this.ActivityFeedService.GetSubscriber(this._identityName);

            return
                updates.Where(
                    update =>
                    update.PublishedBy == subscriber.Id || update.Topics.Any(subscriber.Subscriptions.Contains));
        }

        protected override IQueryable<Update> SecurityTrimUpdates(IQueryable<Update> updates)
        {
            return updates;
        }
    }
}